package com.dreamfire.io.net
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.ObjectEncoding;
	import flash.net.Socket;
	import flash.utils.ByteArray;
	import flash.utils.Endian;

	public class SocketConnector extends Socket
	{
		//当前连接主机地址
		private var _host:String;
		//当前连接主机端口
		private var _port:int;
		
		//成功连接时执行的方法
		private var _handleFunction:Function;
		//IO错误时调用的方法
		private var _ioErrorFunction:Function;
		//丢失当前连接时执行的方法
		private var _loseConnectFunction:Function;
		
		//包头长度 默认为4字节
		private var _headLen:int = 4;
		//是否已读取过包头
		private var _headRead:Boolean = false;
		//当前包体长度
		private var _packageLen:uint = 0;
		
		public function SocketConnector(host:String = null, port:int = 0, fun:Function = null, ioeFun:Function = null, lcFun:Function = null)
		{
			super();
			
			_host = host;
			_port = port;
			
			_handleFunction = fun;
			_ioErrorFunction = ioeFun;
			_loseConnectFunction = lcFun;
			
			this.endian = Endian.LITTLE_ENDIAN;
		}
		
		public override function connect(host:String, port:int):void
		{
			_host = host;
			_port = port;
			
			if(_host == null || _host == "" || _port == 0)
			{
				return
			}
			
			this.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandle);
			this.addEventListener(ProgressEvent.SOCKET_DATA, onDataHandle);
			this.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandle);
			this.addEventListener(Event.CLOSE, closeHandle);
			this.addEventListener(Event.CONNECT, connectHandle);
			
			super.connect(_host, _port);
			
		}
		
		public function set host(p_value:String):void
		{
			_host = p_value;
		}
		
		public function set port(p_value:int):void
		{
			_port = p_value;
		}
		
		public function set handleFunction(p_value:Function):void
		{
			_handleFunction = p_value;
		}
		
		public function send(data:Object):void
		{
			if (this.connected)
			{
				var byte:ByteArray = new ByteArray();
				byte.objectEncoding = ObjectEncoding.AMF3;
				byte.writeObject(data);
				var len:int = byte.length;
				this.writeInt(len);
				this.writeBytes(byte, 0, byte.length);
				this.flush();
			}
		}
		
		private function onDataHandle(event:ProgressEvent):void
		{
			while(this.bytesAvailable > 0)
			{
				if(!_headRead && this.bytesAvailable >= _headLen)
				{
					_packageLen = this.readInt();
					_headRead = true;
				}else{
					if(this.bytesAvailable >= _packageLen)
					{
						var content:ByteArray = new ByteArray();
						this.readBytes(content, 0, _packageLen);
						var data:Object;
						data = content.readObject();
						_handleFunction(data);
						_headRead = false;
					}else{
						break;
					}
				}
			}
		}
		
		private function connectHandle(event:Event):void
		{
			
		}
		
		private function ioErrorHandle(event:IOErrorEvent):void
		{
		}
		
		private function closeHandle(event:Event):void
		{
		}
		
		private function securityErrorHandle(event:SecurityErrorEvent):void
		{
		}
	}
}